package com.example.side_pull_menu.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.side_pull_menu.Fragmentkj1;
import com.example.side_pull_menu.Fragmentkj1;
import com.example.side_pull_menu.R;
import com.google.gson.Gson;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

public class Fragment_1 extends Fragment {
    private TextView tv_amount;
    private TextView tv_time;
    private TextView tv_who;
    private TextView tv_carno;
    private TextView tv_charge;
    private Context mContext;
    private ListView listView;
    private List<Fragmentkj1> listView1 = new ArrayList<>();
    private Handler mHandler =  new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Gson gson = new Gson();
                    Fragmentkj1 carlist = gson.fromJson(msg.obj.toString(), Fragmentkj1.class);
                    listView1.add(carlist);
                    listView.setAdapter(new MyAdapter(listView1));
            }
        }
    };
    public Fragment_1() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view1 = inflater.inflate(R.layout.fragment_1, container, false);
        initView(view1);
        return view1;
    }
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return super.onCreateAnimation(transit, enter, nextAnim);
    }
    private void initView(View view1) {
        //获取控件
        mContext=getContext();
        tv_amount = (TextView)view1.findViewById(R.id.tv_amount);
        tv_time = (TextView) view1.findViewById(R.id.tv_time);
        tv_who = (TextView)view1. findViewById(R.id.tv_who);
        tv_charge = (TextView)view1. findViewById(R.id.tv_charge);
        tv_carno = (TextView)view1. findViewById(R.id.tv_carno);
        listView = view1.findViewById(R.id.lv);
        sedRequestWithOkHttp();
    }
    private void sedRequestWithOkHttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //首先创建一个okHttpClient
                    OkHttpClient client = new OkHttpClient();
                    //如果想要发送一条HTTp就要创建一个Request对象
                    Request request = new Request.Builder()
                            .url("http://hh1.me:5001/getchargelog")
                            .build();
                    //创建一个call对象，调用execute方法来发送请求并获取服务器的返回数据，response就是服务器返回的数据
                    Response response = client.newCall(request).execute();
                    //把服务器返回的数据转换成String类型来得到具体内容
                    String responseData = response.body().string();
                    //创建一个Handler对象
                    Message meg = new Message();
                    meg.what = 1;
                    meg.obj = responseData;
                    mHandler.sendMessage(meg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    class MyAdapter extends BaseAdapter {
        List<Fragmentkj1> list;

        public MyAdapter(List<Fragmentkj1> fragmentkj11){
            list = fragmentkj11;
        }

        @Override
        //告诉集合要显示多少数据
        public int getCount() {
            Log.i(TAG, "getCount: "+list.get(0).getData().size()+"");

            return list.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        //告诉listview每个条目显示的内容
        public View getView(int i, View view, ViewGroup viewGroup) {
            Collections.sort(listView1.get(0).getData(),new sort());
            View view1 = (View) View.inflate(getContext(),R.layout.fragment_1,null);
            TextView time = view1.findViewById(R.id.tv_time);
            TextView who = view1.findViewById(R.id.tv_who);
            TextView charge = view1.findViewById(R.id.tv_charge);
            TextView amount = view1.findViewById(R.id.tv_amount);
            TextView carno = view1.findViewById(R.id.tv_carno);
            who.setText(list.get(0).getData().get(i).getWho());
            time.setText( list.get(0).getData().get(i).getTime());
            charge.setText(""+list.get(0).getData().get(i).getCharge());
            amount.setText(""+list.get(0).getData().get(i).getAmount());
            carno.setText(list.get(0).getData().get(i).getCarno());
            return view1;
        }
        //集合排序
        public class sort implements Comparator<Fragmentkj1.DataBean>{
            @Override
            public int compare(Fragmentkj1.DataBean f0, Fragmentkj1.DataBean f1) {
                return f1.getTime().compareTo(f0.getTime());
            }
        }
    }
}

